package com.notelapierdas.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.notelapierdas.exceptions.BusinessAlreadyExistsException;
import com.notelapierdas.objects.Address;
import com.notelapierdas.objects.Business;

public class BusinessDB {

	private SQLiteDatabase db;
	private DBHelper helper;

	private static final String TABLE_NAME = "Business";
	private static final String ID = "_id";
	private static final String NAME = "name";
	private static final String ADDRESS = "address";
	private static final String COORDENATES = "coordenates";
	private static final String URL = "url";
	private static final String EMAIL = "email";
	private static final String TELEPHONE = "telephone";

	private static final String CREATE_BUSINESSDB = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME
			+ " ( "
			+ ID
			+ " INTEGER PRIMARY KEY, "
			+ NAME
			+ " TEXT NOT NULL, "
			+ ADDRESS
			+ " TEXT NOT NULL, "
			+ COORDENATES
			+ " TEXT NOT NULL, "
			+ URL
			+ " TEXT NOT NULL, "
			+ EMAIL
			+ " TEXT NOT NULL, " + TELEPHONE + " TEXT NOT NULL)";

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_BUSINESSDB);
	}

	public BusinessDB(DBHelper helper) {
		this.helper = helper;
	}

	public void open() throws SQLiteException {
		db = helper.getWritableDatabase();
	}

	public void close() {
		helper.close();
	}

	public int saveBusiness(String name, String address, String coordenates,
			String url, String email, String telephone)
			throws BusinessAlreadyExistsException {
		int id = -1;
		businessExists(name, address);
		try {
			open();

			ContentValues content = new ContentValues();
			content.put(NAME, name);
			content.put(ADDRESS, address);
			content.put(COORDENATES, coordenates);
			content.put(URL, url);
			content.put(EMAIL, email);
			content.put(TELEPHONE, telephone);

			id = (int) db.insert(TABLE_NAME, null, content);

		} catch (SQLiteException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return id;
	}

	private void businessExists(String name, String address)
			throws BusinessAlreadyExistsException {
		try {
			open();
			String[] selectionArgs = new String[] { NAME, ADDRESS };
			Cursor cursor = db.query(TABLE_NAME, null, NAME + "=? AND "
					+ ADDRESS + "=?", selectionArgs, null, null, null);
			if (cursor.getCount() > 0) {
				throw new BusinessAlreadyExistsException();
			}
		} catch (SQLiteException e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	public int getID(String name, String address) {
		int id = -1;
		try {
			open();
			String[] selectionArgs = new String[] { NAME, ADDRESS };
			Cursor cursor = db.query(TABLE_NAME, null, NAME + "=? AND "
					+ ADDRESS + "=?", selectionArgs, null, null, null);
			cursor.moveToFirst();
			id = cursor.getInt(0);
		} catch (SQLiteException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return id;
	}
	
	public Business getBusinessById(int id){
		Business business = null;
		
		try{
			open();
			
			Cursor cursor = db.query(TABLE_NAME, null, ID + "="+id, null, null, null, null);
			cursor.moveToFirst();
			String name = cursor.getString(cursor.getColumnIndex(NAME));
			String aName = cursor.getString(cursor.getColumnIndex(ADDRESS));
			String coor = cursor.getString(cursor.getColumnIndex(COORDENATES));
			Address address = new Address(aName, coor);
			String url = cursor.getString(cursor.getColumnIndex(URL));
			String email = cursor.getString(cursor.getColumnIndex(EMAIL));
			String telephone = cursor.getString(cursor.getColumnIndex(TELEPHONE));
			business = new Business(name, address, url, email, telephone);
		}catch(SQLiteException e){
			e.printStackTrace();
		}finally{
			close();
		}
		return business;
	}

}
